﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;

namespace Ecobee.Net
{
	/// <summary>
	/// 
	/// </summary>
	public class Api
	{
		private ThermostatSummary _thermostatSummary = null;
		private Thermostats _thermostats = null;

		/// <summary>
		/// 
		/// </summary>
		public Api()
		{
			this.BaseUri = "https://api.ecobee.com";
			this.Version = 1;
			this.Timeout = TimeSpan.FromSeconds(30);
			this.KnownUri = new KnownUri(this);
			this.Http = new Http(this);
			this.Access = new Access(this);
			this.ThermostatSummary = new ThermostatSummary(this);
			this.Thermostats = new Thermostats(this);
		}

		/// <summary>
		/// Gets a ThermostatSummary instance used for polling on the API.
		/// </summary>
		[JsonIgnore]
		public ThermostatSummary ThermostatSummary
		{
			get
			{
				return _thermostatSummary;
			}
			private set
			{
				_thermostatSummary = value;
			}
		}

		/// <summary>
		/// Gets a Ecobee.Net.Thermostats instance that can be used for caching
		/// and retrieving details for each thermostat.
		/// </summary>
		[JsonIgnore]
		public Thermostats Thermostats
		{
			get
			{
				return _thermostats;
			}
			private set
			{
				_thermostats = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public KnownUri KnownUri { get; protected set; }

		/// <summary>
		/// Gets/sets the base URL for the API.
		/// </summary>
		public string BaseUri { get; set; }

		/// <summary>
		/// Gets/sets the version of the API to use.
		/// </summary>
		public int Version { get; set; }

		/// <summary>
		/// Gets/sets the the unique application key for your application.
		/// </summary>
		public string ApplicationKey { get; set; }

		/// <summary>
		/// Gets/sets the name of the application.
		/// </summary>
		public string ApplicationName { get; set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="access"></param>
		/// <returns></returns>
		public Dictionary<string, string> DefaultParameters(Access access)
		{
			Dictionary<string, string> returnValue = new Dictionary<string, string>();

			returnValue.Add("client_id", this.ApplicationKey);
			returnValue.Add("format", "json");

			if (access.IsValid)
			{
				returnValue.Add("token", access.AccessToken.Identifier);
			}

			return returnValue;
		}

		/// <summary>
		/// Gets/sets the URL of the API
		/// </summary>
		public string ApiUri
		{
			get
			{
				return string.Format("{0}/{1}", this.BaseUri, this.Version);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="baseUri"></param>
		/// <param name="parameters"></param>
		/// <returns></returns>
		public string BuildUri(string baseUri, Dictionary<string, string> parameters)
		{
			string returnValue = string.Empty;

			if (parameters.Count() > 0)
			{
				StringBuilder newUri = new StringBuilder();
				int i = 0;

				foreach (var item in parameters)
				{
					if (i == 0 && !baseUri.Contains("?"))
					{
						newUri.Append("?");
					}
					else
					{
						newUri.Append("&");
					}

					newUri.Append(string.Format("{0}={1}", item.Key, item.Value));
					i++;
				}

				returnValue = string.Format("{0}{1}", baseUri, newUri.ToString());
			}
			else
			{
				returnValue = baseUri;
			}


			return returnValue;
		}

		/// <summary>
		/// Returns an Http instance that represents the underlying layer
		/// that manages interaction with the ecobee API. This reference
		/// can be used to cancel any pending requests.
		/// </summary>
		public Http Http { get; protected set; }

		/// <summary>
		/// 
		/// </summary>
		public Access Access { get; protected set; }

		/// <summary>
		/// The amount of time to wait for a HTTP operation to complete before
		/// throw a timeout exception.
		/// </summary>
		public TimeSpan Timeout { get; protected set; }
	}
}
